#include "stdafx.h"
#include "SoundManager.h"

#include "Log.h"
#include "Exception.h"

#include "bass.h"

namespace Cvekas {

SoundManager::SoundManager(HWND window_handle)
{
	if(!BASS_Init(-1, 44100, 0, window_handle, NULL))
		throw EXCEPTION(FORMAT("Unable to initialize BASS. Error code: %d", % BASS_ErrorGetCode()));

	LOG("SoundManager created");
}

SoundManager::~SoundManager()
{
	// All sound resources must be freed before calling BASS_Free()
	resources.clear();

	if(!BASS_Free())
		throw EXCEPTION(FORMAT("Unable to free BASS. Error code: %d", % BASS_ErrorGetCode()));

	LOG("SoundManager destroyed");
}

SoundPtr SoundManager::loadSample(const std::string& name, const std::string& filename)
{
	ResourceId id = getUniqueId(name);
	SoundPtr sound(new SoundSample(name, id, filename));
	return insert(name, sound, id);
}

SoundPtr SoundManager::loadStream(const std::string& name, const std::string& filename)
{
	ResourceId id = getUniqueId(name);
	SoundPtr sound(new SoundStream(name, id, filename));
	return insert(name, sound, id);
}

void SoundManager::update()
{
	BASS_Update(100);
}

} // namespace